import { createAsyncThunk, createSlice } from "@reduxjs/toolkit"
import axios from "axios"
import { checkout } from "../shoppingCart/slice"

interface OrderState {
  loading: boolean,
  error: string | null,
  currentOrder: any
}

const initialState: OrderState = {
  loading: false,
  error: null,
  currentOrder: null
}

export const placeOrder = createAsyncThunk(
  'order/placeOrder',
  async (parameters:{jwt:string,orderId:string}, thunkAPI) => {
    const { data } = await axios.post(`http://82.157.43.234:8080/api/orders/${parameters.orderId}/placeOrder`,null, {
      headers: {
        'x-icode': 'EDE45279EB142953',
        "Authorization": "Bearer " + parameters.jwt
      }
    })
    return data

  }
)


export const orderSlice = createSlice({
  name: 'order',
  initialState,
  reducers: {},
  extraReducers: (builder) => {
    builder
      .addCase(placeOrder.pending, (state) => {
        state.loading = true
      })
      .addCase(placeOrder.fulfilled, (state, action) => {
        state.currentOrder = action.payload
        state.loading = false
        state.error = null
      })
      .addCase(placeOrder.rejected, (state, action) => {
        state.loading = false
        if (typeof action.payload === 'string') {
          state.error = action.payload
        } else {
          state.error = null
        }

      })
      .addCase(checkout.pending, (state) => {
        state.loading = true
      })
      .addCase(checkout.fulfilled, (state, action) => {
        state.currentOrder = action.payload
        state.loading = false
        state.error = null
      })
      .addCase(checkout.rejected, (state, action) => {
        state.loading = false
        if (typeof action.payload === 'string') {
          state.error = action.payload
        } else {
          state.error = null
        }

      })
  }
})